<template>
    <div class="goods-comment" >
    <div class="head" v-if="commentData">
      <div class="data">
        <p><span>{{commentData.salesCount}}</span><span>人购买</span></p>
        <p><span>{{commentData.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a href="javascript:;" :class="{active:tagIndex === i}" v-for="(tag,i) in commentData.tags" :key="i" @click="changeTag(i)">{{tag.title}} ({{tag.tagCount}})</a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" @click="changeSort(null)" :class="{active:reqParams.sortField === null}">默认</a>
      <a href="javascript:;" @click="changeSort('praiseCount')" :class="{active:reqParams.sortField === 'praiseCount'}">最新</a>
      <a href="javascript:;" @click="changeSort('createTime')" :class="{active:reqParams.sortField === 'createTime'}">最热</a>
    </div>
    <!-- 列表 -->
     <div class="list" v-if="TagCommentData">
      <div class="item" v-for="item in TagCommentData" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" >
          <span>{{Nickname(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in item.score" :key="i"></i>
            <i class="iconfont icon-wjx02" v-for="i in 5-item.score" :key="i"></i>
            <span class="attr">{{Specs(item.orderInfo.specs)}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <GoodsCommentImage :picture="item.pictures"/>
          <div class="time">
            <span>2020-10-10 10:11:22</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
    <XtxPagination @change-Page="changePageFn" :counts="counts" :pageSize="pageSize" :pages="pages" :page="page"/> 
  </div>
</template>

<script>
import GoodsCommentImage from './goodsCommentImage.vue'
import { inject, reactive, ref, watch } from 'vue'
import { findCommentData, findCommentList } from '@/api/goods'
import { useRoute } from 'vue-router'
export default {
    name:'GoodsComment',
    components:{GoodsCommentImage},
    setup(){
        const detailGoods = inject('goods')
        // 获取后台评价信息
        const route = useRoute()
        const commentData = ref(null)
        findCommentData(route.params.id).then(data=>{
            // console.log(data.result,'data.result')
            data.result.tags.unshift({title:'有图',tagCount:data.result.hasPictureCount,type:'img'})
            data.result.tags.unshift({title:'全部评价',tagCount:data.result.evaluateCount,type:'all'})
            commentData.value = data.result
            // 传给子组件的数据  
        })
        const reqParams = reactive({
            page:1,
            pageSize:10,
            hasPicture:null,
            tag:null,
            sortField:null
        })
        // watch对数据进行监听
        const TagCommentData = ref([])
        // 初始化发请求
        // 总数量
        const counts = ref(1)
        // 每页的条数
        const pageSize = ref(1)
        // 总页数
        const pages = ref(1)
        // 当前页数
        const currCounts = ref(1)
        watch(reqParams,()=>{
            findCommentList(detailGoods.id,reqParams).then(data => {
            console.log(data.result.items,'items')
            TagCommentData.value = data.result.items
            counts.value = data.result.counts
            pageSize.value = data.result.pageSize
            pages.value = data.result.pages
            currCounts.value =data.result.page
            // console.log(page.value,'pageValue')
        },{immediate:true})
        })
        // 实现分页切换
        const changePageFn = (newVal) => {
          reqParams.page  = newVal
        }
        let tagIndex = ref(0)
        reqParams.hasPicture = false
        reqParams.tag = null
        const changeTag = (i) => {
            tagIndex.value = i
            const tag = commentData.value.tags[i]
            // 判断是全部评价还是有图还是tag
            if(commentData.type === 'all'){
                reqParams.hasPicture = false
                reqParams.tag = null
            }else if(commentData.type === 'img'){
                reqParams.hasPicture = true
                reqParams.tag = null
            }else{
                reqParams.hasPicture = false
                reqParams.tag = tag.title
            }
            reqParams.page = 1
        }
        // 名字匿名
        const Nickname = (nickname) => {
           return nickname.slice(0,1) + '******' + nickname.slice(-1) 
        } 
        // 日期
        const Specs = (specs) => {
            return specs.reduce((p, c) => `${p} ${c.name} : ${c.nameValue}`,'').trim()
        }
        // 排序
        const changeSort = (type) => {
        reqParams.sortField = type
        reqParams.page = 1
      }
        return {changePageFn, detailGoods, commentData, tagIndex, changeTag, reqParams, TagCommentData,Nickname, Specs, changeSort,counts,pageSize,pages,currCounts}
    }
    
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor,50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,&:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
</style>